import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    counter: 999
  },
  // 异步方法 使用dispatch分发
  actions: {
    asyncAdd: function (store, step) {
      setTimeout(() => {
        store.commit('add', step)
      }, 1000)
    }
  },
  // 同步方法 使用commit提交
  mutations: {
    add: function (state, step) {
      state.counter += step
    },
    sub: function (state, step) {
      state.counter += step
    }
  },
  getters: {
    isOddOrEven: function (state) {
      return state.counter % 2 === 0 ? '偶数' : '奇数'
    }
  }
})
export default store
